package com.gin.kotlin.no_gay_alliance.runner

import android.util.Log
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.app.AppRunner
import com.gin.kotlin.no_gay_alliance.base.GlideUtils
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.CheckInStatusProvider
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.ReplyListProvider
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.TopicListProvider
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.UserInfoProvider
import com.gin.kotlin.no_gay_alliance.store.RuntimeData
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils
import com.gin.kotlin.no_gay_alliance.worker.ImagePreloadWorker
import java.time.ZonedDateTime

/**
 * 每天执行一次，清理部分缓存
 */
class ClearCacheRunner : AppRunner {
    /**
     * 执行的任务内容
     * @param app [App]
     */
    override fun run(app: App) {
        val runtimeData = RuntimeData.getInstance()
        // 今天的日期
        val date = TimeUtils.DATE_FORMATTER_YEAR_DAY.format(ZonedDateTime.now())
        // 上次执行缓存清理的日期 与今天不同时执行清理
        if (date != runtimeData.clearCacheDate) {
            Log.i(TAG, "执行缓存清理")
            GlideUtils.clearCache(App.INSTANCE)
            TopicListProvider.getInstance().clear()
            ReplyListProvider.getInstance().clear()
            UserInfoProvider.getInstance().clear()
            CheckInStatusProvider.getInstance().clear()

            runtimeData.clearCacheDate = date

            ImagePreloadWorker.delCacheFile(app)
        }
    }

    companion object {
        private val TAG = ClearCacheRunner::class.java.simpleName
    }
}